<%@page import="java.util.ArrayList"%>
<%@ include file="/init.jsp"%>


<%@page import="java.util.StringTokenizer"%>
<%@page import="com.liferay.portal.service.ListTypeServiceUtil"%>
<%@page import="com.liferay.portal.model.ListType"%>
<%@ include file="/init.jsp"%>
<script src="<%=request.getContextPath()%>/js/jquery-1.6.2.js" language="javascript" type="text/javascript"></script>

<portlet:resourceURL var="ajaxCallResourceURL" />

<%
ListType mappedFee = (ListType) request.getAttribute("mappedFee");
System.out.println("doing ID >>>" + mappedFee.getListTypeId());

List<ListType> mappedSubFees = 
	ListTypeServiceUtil.getListTypes
		("com.teqbridgeltd.liferay.model.map.Fees." + mappedFee.getName() );
System.out.println("com.teqbridgeltd.liferay.model.map.Fees." + mappedFee.getName() +"<<<");

%>

		<aui:layout>
			<aui:column>
				<span class="hder" id="feesName_<%=mappedFee.getListTypeId() %>" ><%=mappedFee.getName() %></span>
			</aui:column>
		</aui:layout>
		
		
		<aui:layout>
			<aui:column columnWidth="33"><br>
				<%
					List<ListType> layoutName = ListTypeServiceUtil.getListTypes("com.teqbridgeltd.liferay.model.layout");
				%>
				<strong>Zone/Layout</strong>
				<select disabled="disabled" onchange="autoFill_<%=mappedFee.getListTypeId() %>()" id="layout_<%=mappedFee.getListTypeId() %>" name="layout_<%=mappedFee.getListTypeId() %>" label="" >
				<option style="font-style: italic; color: #A0A0A4" value="">--Please Select Layout--</option>
		  	
		  	<%
		   
		  	for(int i=0; i<layoutName.size(); i++){
		  		ListType _layoutName = layoutName.get(i);
		  	%>	
		  	<option value="<%=_layoutName.getName() %>"><%=_layoutName.getName()  %></option>
		  	<%
		  	}
		  	%>
			</select>
 			</aui:column>
			<aui:column columnWidth="33"><br>	
		 		<%
				List<ListType> propTypes = ListTypeServiceUtil.getListTypes("com.teqbridgeltd.liferay.model.property_type");
				%>
				<strong>Property Type</strong>
				<select id="propertyType_<%=mappedFee.getListTypeId() %>" name="propertyType_<%=mappedFee.getListTypeId() %>"  >
					<option style="font-style: italic; color: #A0A0A4" value="">--Please Select Type--</option>
			  	
			  	<%
			   
			  	for(int i=0; i<propTypes.size(); i++){
			  		ListType _propType = propTypes.get(i);
			  	%>	
			  	<aui:option 
			                		value="<%=_propType.getName() %>"><%=_propType.getName()  %></aui:option>
			  	<%
			  	}
			  	%>
				</select>
			</aui:column>
			<aui:column columnWidth="33">	<br>	
				&nbsp;
			</aui:column>
		</aui:layout>
		
		<aui:layout>
			<aui:column columnWidth="33"><br>	
		 		<%
				List<ListType> useTypes = ListTypeServiceUtil.getListTypes("com.teqbridgeltd.liferay.model.land_use");
				
				%>
				<aui:select name="useType" label="Specify Required Land Use" 
					onChange='<%= renderResponse.getNamespace() + "getPropertyUse(this);" %>'
				 style="width:150px" >
					<option value="">-Please Select Land Use-</option>
			  	
			  	<%
			  	for(int i=0; i<useTypes.size(); i++){
			  		ListType _useType = useTypes.get(i);
			  	%>	
			  	<aui:option value="<%=_useType.getName() %>"><%=_useType.getName()  %></aui:option>
			  	<%
			  	}
			  	%>
				</aui:select>
			</aui:column>
			<aui:column columnWidth="33">	<br>
				<aui:select  id="purpose" name="purpose" label="Specify Purpose"  style="width:150px"></aui:select> 
			</aui:column>
			<aui:column columnWidth="33">	
				&nbsp;
			</aui:column>
		</aui:layout>
			
			
		<aui:layout>
			<aui:column columnWidth="33"><br>	
				<strong>Area (Sq Meters):</strong>
				<input readonly="readonly" onchange="autoFill_<%=mappedFee.getListTypeId() %>()" id="area_<%=mappedFee.getListTypeId() %>" name="area_<%=mappedFee.getListTypeId() %>" type="text"></input>
			</aui:column>
			<aui:column columnWidth="33">	<br>
				
				<%
				
				int startYear = 1983;
				int endYear = 2012;
				
				%>
				
				<aui:select name="lastPaymentYear" label="Year Of Last Payment">
					<option style="font-style: italic; color: #A0A0A4" value="">--Please Select Year Paid--</option>
			  	
			  	<%
			   
			  	for(int year=startYear; year<= endYear; year++){
			  		
			  	%>	
			  	<aui:option value="<%= year %>"><%= year  %></aui:option>
			  	<%
			  	}
			  	%>
				</aui:select>
			</aui:column>
			<aui:column columnWidth="33">	<br><br>
				<input class="resultbox" readonly="readonly" name="result_<%=mappedFee.getListTypeId() %>"  id="result_<%=mappedFee.getListTypeId() %>" type="text" value=""/>
			</aui:column>
		</aui:layout>
		
		<aui:layout>
			<aui:column>
			<button type="button" onClick="ComputeFee_<%=mappedFee.getListTypeId() %>();" name="compute_result" value="Compute">Compute</button>
			</aui:column>
		</aui:layout>
   		
 
 
 <portlet:resourceURL var="fetchdataAction">
</portlet:resourceURL>
<script>
function autoFill_<%=mappedFee.getListTypeId() %>(){
	try{
			
			var i=0;
			var area_Present= document.getElementById('area_' + <%=mappedFee.getListTypeId() %>);
			var Present_layout = document.getElementById('layout_' + <%=mappedFee.getListTypeId() %>);
			var layout_Fixed = document.getElementById('layout_Fixed');
			layout_Fixed.value=Present_layout.value;
			//area_Fixed.value=area_Present.value;
			for (i = 40200; i <= 100000; i++){
				var layout=document.getElementById('layout_' + i);
				
				if (layout !=null && layout !=""){
					for (j = 0; j <= layout.length-1; j++){
						if (layout.options[j].text==layout_Fixed.value){						
							//layout.value=layout_Fixed.value;
							layout.selectedIndex=j;					
						}					
					}			
					}
			
			  }
			
		}catch(err){
			alert(err.message);
		}
	}
function ComputeFee_<%=mappedFee.getListTypeId() %>(){
	try{
		
		var result = document.getElementById('result_<%=mappedFee.getListTypeId() %>');
		var layout = document.getElementById('layout_<%=mappedFee.getListTypeId() %>');
		var propertyType = document.getElementById('propertyType_<%=mappedFee.getListTypeId() %>');
		//alert(propertyType.value);
		var useType = document.getElementById('<portlet:namespace/>useType');
		//alert(useType.value);
		var purpose = document.getElementById('<portlet:namespace/>purpose');
		//alert(purpose.value);
		var area = document.getElementById('area_<%=mappedFee.getListTypeId() %>');
		//alert(area.value);
		var lastPaymentYear = document.getElementById('<portlet:namespace/>lastPaymentYear');
		//alert(lastPaymentYear.value);
		//var purpose =  descPurpose.split(" ",1);
		
		
		<portlet:namespace />getGroundRentRate(layout.value, propertyType.value, useType.value, lastPaymentYear.value,area.value);
		hidePrintButton();
		computeTotal();
	}catch(err){
	  txt="There was an error on this page.\n\n";
	  err.message;
	  txt+="Error description: " + err.description + "\n\n";
	  txt+="Click OK to continue.\n\n";
	  txt+=err.message + "\n\n";
	  alert(txt);
	}
	
}

function <portlet:namespace />getPropertyUse(obj){
	ajaxcallToGetPropertyUse(obj.value);
}


function <portlet:namespace />getGroundRentRate(layout,   propertyType , useType , lastPaymentYear ,area ){
	
	ajaxcallToGetGroundRentRate(layout, propertyType, useType, lastPaymentYear, area);
}


function ajaxcallToGetPropertyUse(landUse)
{
    $.ajax({
       type: "POST",
       url: "<%= fetchdataAction %>",
       data:  {
			<portlet:namespace />landUse: landUse,
			<portlet:namespace />queryType: 'landUse',
		},
		dataType: "json",
       success: function(msg){

    	var message = msg;
    						
		var stateId = message.stateId;
		
		
		document.getElementById('<portlet:namespace />purpose').options.length = 0;
		var oFieldSelect= document.getElementById('<portlet:namespace />purpose');
		var purpose = document.createElement('option');  
		purpose.appendChild(document.createTextNode("-Please specify purpose-"));
		oFieldSelect.appendChild(purpose);
		
		var purposeId = message.purposeId;
		
		document.getElementById('<portlet:namespace/>purpose').options.length = 0;
		var oFieldSelect= document.getElementById('<portlet:namespace/>purpose');
		
		var purpose = document.createElement('option');  
		purpose.appendChild(document.createTextNode("-Please specify purpose-"));
		oFieldSelect.appendChild(purpose);
		var j=0;
		if (purposeId>0){
			if (message) {
			  	for( i=1; i<=purposeId; i++){
				    var param = "param_" + i;
				    var purposeCode = "purposeCode_" + i;
					//alert(param);
					var oFieldSelect= document.getElementById('<portlet:namespace/>purpose');
					var purpose = document.createElement('option'); 
		
					//purpose.appendChild(document.createTextNode(message." + param + ")); 
					purpose.appendChild(document.createTextNode(message[purposeCode]));
					
					oFieldSelect.appendChild(purpose); 
					
					for( j=0; j<=oFieldSelect.length-1; j++){
						//alert(oFieldSelect.options[j].text);
						//alert(landUse);
						if (oFieldSelect.options[j].text==landUse){
							 oFieldSelect.selectedIndex=j;
						}
					}
				
			  }
			 
				
			}
			else {
				alert('no data back from server');
			}
		}else{
			var oFieldSelect= document.getElementById('<portlet:namespace/>purpose');
				var purpose = document.createElement('option');  
				purpose.appendChild(document.createTextNode("-Not available-"));
				oFieldSelect.appendChild(purpose); 
		}
       }
     });
}

function ajaxcallToGetGroundRentRate(layout, propertyType, useType, lastPaymentYear, area )
{
    $.ajax({
       type: "POST",
       url: "<%= fetchdataAction %>",
       data: {
			<portlet:namespace />layout: layout,
			<portlet:namespace />propertyType: propertyType,
			<portlet:namespace />useType: useType,
			<portlet:namespace />lastPaymentYear: lastPaymentYear,
			<portlet:namespace />area: area,
			<portlet:namespace />queryType: 'groundRentRate',
		},
		dataType: "json",
       success: function(msg){

    	var message = msg;
    						
    	var penalty = message.penalty;
		var groundRent = message.groundRent;
	
		if(message){
			document.getElementById('result_<%=mappedFee.getListTypeId() %>').value=groundRent;
		}else {
			alert('no data back from server');
		}
       }
     });
}
</script>

 

<aui:script>
	Liferay.provide(window,
		'<portlet:namespace />getPropertyUseOLD',
		function(landUse) {
	
			var A = AUI();
			var url = '<%= ajaxCallResourceURL.toString() %>';
				A.io.request(
					url,
					{
						//data to be sent to server
						data: {
							<portlet:namespace />landUse: landUse,
							<portlet:namespace />queryType: 'landUse',
						},
						dataType: 'json',

						on: {
							failure: function() {
							},

							success: function(event, id, obj) {
								var instance = this;


								//JSON Data coming back from Server
								
								var message = instance.get('responseData');
								
								var purposeId = message.purposeId;
								
						document.getElementById('<portlet:namespace/>purpose').options.length = 0;
						var oFieldSelect= document.getElementById('<portlet:namespace/>purpose');
						
									var purpose = document.createElement('option');  
									purpose.appendChild(document.createTextNode("-Please specify purpose-"));
									oFieldSelect.appendChild(purpose);
									var j=0;
							if (purposeId>0){
								if (message) {
								  for( i=1; i<=purposeId; i++){
								    var param = "param_" + i;
								    var purposeCode = "purposeCode_" + i;
									//alert(param);
									var oFieldSelect= document.getElementById('<portlet:namespace/>purpose');
									var purpose = document.createElement('option'); 
						
									//purpose.appendChild(document.createTextNode(message." + param + ")); 
									purpose.appendChild(document.createTextNode(message[purposeCode]));
									
									oFieldSelect.appendChild(purpose); 
									
									for( j=0; j<=oFieldSelect.length-1; j++){
									//alert(oFieldSelect.options[j].text);
									//alert(landUse);
									if (oFieldSelect.options[j].text==landUse){
									 oFieldSelect.selectedIndex=j;
									}
									}
									
								  }
								 
									
								}
								else {
									alert('no data back from server');
								}
								}
							else{
								var oFieldSelect= document.getElementById('<portlet:namespace/>purpose');
									var purpose = document.createElement('option');  
									purpose.appendChild(document.createTextNode("-Not available-"));
									oFieldSelect.appendChild(purpose); 
								}
							}

						}
					}

				); //END of io Request

		},
		['aui-io']
	);  //End of Provide
	
</aui:script>

<aui:script>
	Liferay.provide(window,
		'getGroundRentRateOLD',
		function(layout, propertyType, useType, lastPaymentYear, area ) {
	
			var A = AUI();
			var url = '<%= ajaxCallResourceURL.toString() %>';
				A.io.request(
					url,
					{
						//data to be sent to server
						data: {
							<portlet:namespace />layout: layout,
							<portlet:namespace />propertyType: propertyType,
							<portlet:namespace />useType: useType,
							<portlet:namespace />lastPaymentYear: lastPaymentYear,
							<portlet:namespace />area: area,
							<portlet:namespace />queryType: 'groundRentRate',
							
							

							
							
						},
						dataType: 'json',

						on: {
							failure: function() {
							},

							success: function(event, id, obj) {
								var instance = this;


								//JSON Data coming back from Server
								
								var message = instance.get('responseData');
								
								var penalty = message.penalty;
								var groundRent = message.groundRent;
								
								
								
								var oFieldSelect= document.getElementById('<portlet:namespace/>purpose');
									var purpose = document.createElement('option');  
									purpose.appendChild(document.createTextNode("-Please specify purpose-"));
									oFieldSelect.appendChild(purpose);
							
								if(message){
									document.getElementById('result_<%=mappedFee.getListTypeId() %>').value=groundRent;
								}else {
									alert('no data back from server');
								}
							}

						}
					}

				); //END of io Request

		},
		['aui-io']
	);  //End of Provide
	
</aui:script>
